package com.vrv.netmgr.usermgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.Context;

import com.vrv.common.GlobalConstants;
import com.vrv.common.UrlPrefix;
import com.vrv.common.util.HttpClientToServer;
import com.vrv.netmgr.persistent.LoginSharedPreferencesHelper;
import com.vrv.netmgr.vo.Classes;
import com.vrv.netmgr.vo.LoginInfo;

/**
 * @Description 用户信息管理
 *
 * @author gaopei
 * @date
 */
public class UserManager {

    private HttpClientToServer httpClientToServer = new HttpClientToServer();
    public static final int LOGIN_SUCCESS = 1;
    public static final int LOGIN_NETERR = 2;
    public static final int LOGIN_INPUTERR = 3;

    public int isRightUser(Context context, Map<String, String> map) {

        String urlPrefix = new UrlPrefix(context).getUrlPrefix();
        String urlAddr = urlPrefix + "/user/login.do";
        String response = httpClientToServer.doPost(urlAddr, map);

        if (HttpClientToServer.DO_POST_ERR.equals(response) || HttpClientToServer.DO_POST_EXCEPT.equals(response))
            return LOGIN_NETERR;
        boolean retVal = false;
        try {
            JSONTokener jsonParser = new JSONTokener(response);
            JSONObject jsonObj = (JSONObject) jsonParser.nextValue();
            retVal = jsonObj.getBoolean("success");
            if (!retVal)
                return LOGIN_INPUTERR;

            retVal = jsonObj.getBoolean("success");
            if (!retVal)
                return LOGIN_INPUTERR;

            // 获取用户userName
            String userID = jsonObj.getJSONObject("record").getString("id").trim();
            String userName = jsonObj.getJSONObject("record").getString("name").trim();
            // 获取当前用户端饿区域和该用户创建的区域
            List<Classes> classes = new ArrayList<Classes>();
            JSONArray array = jsonObj.getJSONObject("record").getJSONArray("classes");
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = (JSONObject) array.get(i);
                String classId = obj.getString("classId");
                String className = obj.getString("className");
                String lever = obj.getString("classIdlevel");
                String parent = obj.getString("upId");

                Classes clazz = new Classes(classId, className, parent, lever);
                classes.add(clazz);
            }
            LoginInfo loginInfo = new LoginInfo(userID, userName, classes);
            GlobalConstants.userInfo = loginInfo;

            LoginSharedPreferencesHelper.saveLoginInfo(context, loginInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return LOGIN_SUCCESS;
    }

    public boolean modifyPasswd(Context context, Map<String, String> map) {
        boolean retVal = false;
        String urlPrefix = new UrlPrefix(context).getUrlPrefix();
        String urlAddr = urlPrefix + "/user/jsonSave.do";
        String response = httpClientToServer.doPost(urlAddr, map);

        if (HttpClientToServer.DO_POST_ERR.equals(response) || HttpClientToServer.DO_POST_EXCEPT.equals(response))
            return retVal;
        try {
            JSONTokener jsonParser = new JSONTokener(response);
            JSONObject jsonObj = (JSONObject) jsonParser.nextValue();
            retVal = jsonObj.getBoolean("success");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return retVal;
    }

}
